package com.hengpeng.itfinoss.controller.reward;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.Activity;
import com.hengpeng.itfinbase.persist.Reward;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.util.CollectionHelp;
/**
 * 活动奖品
 * @author liyangyang
 *
 */
@Controller
@RequestMapping(value="/reward")
public class RewardController extends BaseController {
    @Autowired
    private ServiceFactory serviceFactory;

    private String jspPath(String pageName) {

        return "reward/" + pageName;
    }

    @RequestMapping(value = "/load/{id}")
    public String ossConfig(ModelMap map, HttpServletRequest request, @PathVariable Long id) {
        Reward reward = serviceFactory.getPersistService().get(Reward.class, id);
        map.put("reward", reward);
        return jspPath("/detail");
    }

    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, ModelMap map) {

        Pagination page = new Pagination(request);
        page.setPageSize(this.getPageSize(request));
        try {
            List<Reward> list = new ArrayList<Reward>();
            ServiceResult<List<Reward>> result = serviceFactory.getRewardService().selectRewardPage(page.currentPage,
                    page.pageSize);
            page.setEntityCount(result.getReturnListSize());
            list = result.getReturnValue();
            map.put("rewardList", list);
            map.put("rewardListPage", page);
            return "/reward/list";
        } catch (Exception ex) {
            ex.printStackTrace();
            return returnError(map, ex);
        }
    }

    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, @ModelAttribute Reward reward, ModelMap map, HttpSession session) {
        System.out.println(reward);
        try {
            reward.setUpdateTime(new Date());
            serviceFactory.getPersistService().update(reward);
        } catch (Exception e) {
            e.printStackTrace();
            return PageHandlerUtil.returnError(map, e);
        }
        return PageHandlerUtil.returnSuccess(map, "/reward/list");
    }

    @RequestMapping(value = "/create")
    public String initSave(ModelMap map) {
        map.put("reward", new Reward());
        return "reward/create";
    }

    @RequestMapping(value = "/onsave")
    public String save(@ModelAttribute Reward reward, ModelMap map, HttpSession session) {
        try {
            // 校验数据无误，进入持久化操作
            serviceFactory.getRewardService().save(reward);
            return PageHandlerUtil.returnSuccess(map, "/reward/list");
        } catch (Exception ex) {
            ex.printStackTrace();
            return PageHandlerUtil.returnError(map, ex);
        }
    }

}

